// model.js

var areautils = require('../../utils/area_data.js')
var area = require('../../utils/area.js')
var areaInfo = [];//所有省市区县数据

var provinces = [];//省

var citys = [];//城市

var countys = [];//区县

var value = [0,0,0];//数据位置下标


//滑动事件
function pickScroll(e, that, callBack) {
  var val = e.detail.value
  //判断滑动的是第几个column
  //若省份column做了滑动则定位到地级市和区县第一位
  if (value[0] != val[0]) {
    val[1] = 0;
    val[2] = 0;
    //获取地级市数据
    citys = areautils.shiArray(provinces[val[0]].value);
    //获取区县数据
    countys = areautils.xianArray(provinces[val[0]].value,citys[val[1]].value);
    
  } else {    //若省份column未做滑动，地级市做了滑动则定位区县第一位
    if (value[1] != val[1]) {
      val[2] = 0;
      //获取区县数据
      countys = areautils.xianArray(provinces[val[0]].value, citys[val[1]].value);
    }
  }
  value = val;
  var show = true;
  var result = {
    animation: that.animation.export(),
    show: show,
    provinces: provinces,
    citys: citys,
    countys: countys,
    value: value
  }
  callBack(result)

}

//初始化并弹起来
function pickup(that, shengId,shiId,xianId,callBack) {

 // animationEvents(that, 200, false, 0);
  value = [0,0,0];
  var shengIndex = 0
  var shiIndex = 0
  var  xianIdex = 0;
  provinces = areautils.shengArray();
  citys = areautils.shiArray(shengId);
  countys = areautils.xianArray(shengId, shiId);

  
  if (shengId != undefined && shengId != ""){
      for(var i=0;i<provinces.length;i++){
          if(provinces[i].value == shengId){
            shengIndex = i;
            break
          }
      }
  }

  if (shiId != undefined && shiId != "") {
    for (var i = 0; i < citys.length; i++) {
      if (citys[i].value == shiId) {
        shiIndex = i;
        break
      }
    }
  }

  if (xianId != undefined && xianId != "") {
    for (var i = 0; i < citys.length; i++) {
      if (countys[i].value == xianId) {
        xianIdex = i;
        break
      }
    }
  }


  var show = true;
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 400,
    timingFunction: "ease",
    delay: 0
  })
  that.animation.translateY(0 + 'vh').step()
 
  var result = {
    animation: that.animation.export(),
    show: show,
    provinces: provinces,
    citys: citys,
    countys: countys,
    value: value
  }
  callBack(result)
}


//model.animationEvents(this, 200, false, 400);

function pickDown(that,callBack){
  animationEvents(that,200,false,400);
  var result = {
    animation: that.animation.export(),
    show: false,
    provinces: provinces,
    citys: citys,
    countys: countys,
    value: value
  }
  callBack(result)
}


//动画事件
function animationEvents(that, moveY, show, duration) {
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: duration,
    timingFunction: "ease",
    delay: 0
  })
  that.animation.translateY(moveY + 'vh').step()


}

module.exports = {
  
  pickup:pickup,
  pickScroll, pickScroll, 
  pickDown: pickDown
}
